/**
 * - 封装获取会员信息的业务流程
 * ----------------------
 */
import { getAccountInfoById, getCardByCustomerId, getCustomerById, accountInfoByCustomerId, getVipAccountList } from "@/api";
import businessStore from "@/store/modules/business";

// 会员账户详情（x）
export const getDetails = async (id) => {
  console.warn("该方法即将被废弃，请使用 getDetailsByCustomerId!!");
  console.log("getPageData:>", id);
  try {
    const pam = {
      accountType: "1",
      customerAccountId: id,
    };
    const { data, success } = await getAccountInfoById(pam);
    // console.log(data);
    if (!success) throw new Error("获取数据失败");
    // 获取到客户id后，再获取客户信息
    if (data.customerId) {
      const cardInfo = await getCardInfo(data.customerId);
      const customerInfo = await getCustomerInfo(data.customerId);
      return {
        customerInfo,
        cardInfo,
        accountInfo: data,
      };
    }
  } catch (error) {
    console.error(error);
  }
};

// 账户详情，查询源头为 客户id，而不是账户id
export const getDetailsByCustomerId = async (id) => {
  // console.log("getPageData:>", id);
  // 商家信息
  const { businessId } = businessStore.state.businessInfo;
  // console.log(">>>> businessId:", businessId, businessStore);
  try {
    const pam = {
      // 	accountType 账户类型（1老人账户、余额账户、积分账户，2志愿者时间币账户 3老人服务补贴账户 5会员卡套餐）
      accountType: "1",
      customerId: id,
      businessId,
    };
    const { data, success } = await accountInfoByCustomerId(pam);
    // console.log(data);
    if (!success) throw new Error("获取数据失败");
    // 获取到客户id后，再获取客户信息
    const cardInfo = await getCardInfo(id);
    const customerInfo = await getCustomerInfo(id);
    return {
      customerInfo,
      cardInfo,
      accountInfo: data,
    };
  } catch (error) {
    console.error(error);
    return null;
  }
};

// 会员卡详情
export const getCardInfo = async (id) => {
  try {
    const pam = { customerId: id };
    const { data, success } = await getCardByCustomerId(pam);
    // console.log("getCardByCustomerId:", data);
    if (!success) throw new Error("获取会员卡信息数据失败");
    return data;
  } catch (error) {
    console.error(error);
    return error;
  }
};

// 会员客户详情
export const getCustomerInfo = async (id) => {
  try {
    const { data, success } = await getCustomerById(id);
    // console.log("getCustomerByUserId:", data);
    if (!success) throw new Error("获取数据失败");
    // customerInfo.value = data;
    // console.log("pageChannel customer:", customerInfo.value);
    return data;
  } catch (error) {
    console.error(error);
    return error;
  }
};

/**
 * 从vipCustomerAccountPage列表查询 单个 会员信息 （当前后端未出单个查询接口，故使用此方式查询.）
 *  vipCustomerAccountPage 的会员信息是最详细的，消费机有较多场景需要使用，但大部分场景只需要查询单个会员信息，故封装此方法
 * @param physicalCardNumber 物理卡号
 * @returns 
 */
// 
export const getVIPAccountByPhysicalCard = async (physicalCardNumber: string) => {
  if (!physicalCardNumber) throw new Error("物理卡号必传");
  try {
    const pam = {
      physicalCardNumber,
      current: 1,
      size: 1,
    };
    const { data, success } = await getVipAccountList(pam);
    if (!success) throw new Error("获取数据失败");
    if(!data.records || data.records.length === 0) throw new Error("未查询到数据");
    // 返回查询到的数据
    return data.records[0];
  } catch (error) {
    console.error(error);
    uni.showToast({
      title: error.message,
      icon: "none",
    });
    throw error;
  }
};
